import { requestClient } from '#/api/request';

// 租户数据类型定义
export interface TenantItem {
  id: string;
  name: string;
  code: string;
  domain?: string;
  is_active: boolean;
  max_users: number;
  expire_at?: string;
  created_at: string;
  updated_at: string;
}

// 租户列表查询参数
export interface TenantListParams {
  page: number;
  size: number;
  name?: string;
  code?: string;
  is_active?: boolean;
  domain?: string;
}

// 租户列表响应结果
export interface TenantListResult {
  items: TenantItem[];
  total: number;
  page: number;
  size: number;
}

// 创建租户请求参数
export interface CreateTenantRequest {
  name: string;
  code: string;
  domain?: string;
  is_active?: boolean;
  max_users?: number;
  expire_at?: string;
}

// 更新租户请求参数
export interface UpdateTenantRequest extends CreateTenantRequest {
  id: string;
}

// 租户API接口
export const tenantApi = {
  // 获取租户列表
  getList: (params: TenantListParams): Promise<TenantListResult> =>
    requestClient.get('/api/tenants', { params }),

  // 获取租户详情
  getDetail: (id: string): Promise<TenantItem> =>
    requestClient.get(`/api/tenants/${id}`),

  // 创建租户
  create: (data: CreateTenantRequest): Promise<TenantItem> =>
    requestClient.post('/api/tenants', data),

  // 更新租户
  update: (data: UpdateTenantRequest): Promise<TenantItem> =>
    requestClient.put(`/api/tenants/${data.id}`, data),

  // 删除租户
  delete: (id: string): Promise<void> =>
    requestClient.delete(`/api/tenants/${id}`),

  // 批量删除租户
  batchDelete: (ids: string[]): Promise<void> =>
    requestClient.delete('/api/tenants/batch', { data: { ids } }),

  // 启用/禁用租户
  toggleStatus: (id: string, is_active: boolean): Promise<TenantItem> =>
    requestClient.put(`/api/tenants/${id}/status`, { is_active }),

  // 检查租户编码是否可用
  checkCode: (code: string, excludeId?: string): Promise<{ available: boolean }> =>
    requestClient.get('/api/tenants/check-code', { 
      params: { code, exclude_id: excludeId } 
    }),
};